package pe.com.modulo.cliente;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import com.sunarp.vehiculo.WSInfoSunarpProxy;

@SuppressWarnings("serial")
@WebServlet("/buscarvehiculo")
public class vehiculoServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sPlaca = request.getParameter("strPlaca");

		Gson gson = new Gson();
		String json = null;

		String data = obtenerDatosDeVehiculo(sPlaca);
		vehiculoBean vehiculo = new vehiculoBean();
		
		String datosArray[] = data.split("&");
		
		vehiculo.setPlaca(datosArray[0].toString());
		vehiculo.setModelo(datosArray[1].toString());
		vehiculo.setMarca(datosArray[2].toString());
		vehiculo.setAnio(datosArray[3].toString());
		vehiculo.setColor(datosArray[4].toString());
		
//		if(data==true){	
//			vehiculo.setPlaca(proxy.mostrarPlaca());
//			vehiculo.setModelo(proxy.mostrarModelo());
//			vehiculo.setMarca(proxy.mostrarMarca());
//			vehiculo.setAnio(proxy.mostrarAnio);
//			vehiculo.setColor(proxy.mostrarColor());
//			json = gson.toJson(vehiculo);
//		}
//		
//		if(data ==false){
//			json = gson.toJson("Placa no esta registrado en SUNARP");
//		}

		json = gson.toJson(vehiculo);

		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();		
		out.print(json);
		out.flush();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("mmmmmmmmmmmmmm");
	}
	
	public String obtenerDatosDeVehiculo(String vPlaca){
		
		WSInfoSunarpProxy proxy = new WSInfoSunarpProxy();
		
		String data = null;
		
		try {
			
			data = proxy.buscarVehiculoPorPlaca(vPlaca);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		return data;
	}
	
}
